package com.zhiziai.aiproxy.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: liuxinyu06
 * @description:
 * @create: 2021-10-27
 **/
@Configuration
@Slf4j
public class TheadPoolConfig {

    public static final ThreadPoolExecutor uploadThreadPoll = new ThreadPoolExecutor(8, 16, 1, TimeUnit.SECONDS, new LinkedTransferQueue<>());
    public static final ThreadPoolExecutor text2ImgThreadPoll = new ThreadPoolExecutor(8, 16, 1, TimeUnit.SECONDS, new LinkedTransferQueue<>());
    public static ThreadPoolExecutor msgExcutor = new ThreadPoolExecutor(4, 8, 0, TimeUnit.MILLISECONDS,
            new SynchronousQueue<>(),new ThreadFactoryBuilder().setNameFormat(
            "msgExcutor-%d").build(),(Runnable r, ThreadPoolExecutor executor) -> log.error(
            "msgExcutor 被抛弃"));

    @Bean
    public ThreadPoolExecutor threadPool() {
        final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(500, 1000, 1, TimeUnit.SECONDS, new LinkedTransferQueue<>());
        return threadPoolExecutor;
    }

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(10);
        threadPoolTaskScheduler.setRemoveOnCancelPolicy(true);
        return threadPoolTaskScheduler;
    }



//    @Bean
//    public ThreadPoolTaskExecutor threadPool() {
//        final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(100);
//        executor.setMaxPoolSize(200);
//        executor.setQueueCapacity(100000);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
//        executor.setKeepAliveSeconds(1);
//        executor.setThreadFactory(new CustomizableThreadFactory("push_ticker_orderBook"));
//        return executor;
//    }

}
